import { defineStore } from 'pinia'
import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData,
  uploadImg
} from '@/service/page-content/page-content'
interface IMain {
  [key: string]: any
}
const usePageContentStore = defineStore('pageContent', {
  state: (): IMain => ({
    // 为什么其他的变量无,因为手动创建麻烦,后续用的自动创建的具体看getPageInfoByPageNameAction方法
    rolePageContenList: [],
    rolePageListTotal: 0,
    roleOffset: 0,
    roleSize: 10,
    usersPageContenList: [],
    usersPageListTotal: 0,
    usersOffset: 0,
    usersSize: 10,
    departmentPageContenList: [],
    departmentPageListTotal: 0,
    departmentOffset: 0,
    departmentSize: 10,
    menuPageContenList: [],
    menuPageListTotal: 0,
    menuOffset: 0,
    menuSize: 10,
    categoryPageContenList: [],
    categoryPageListTotal: 0,
    categoryOffset: 0,
    categorySize: 10,
    goodsPageContenList: [],
    goodsPageListTotal: 0,
    goodsOffset: 0,
    goodsSize: 10,
    logPageContenList: [],
    logPageListTotal: 0,
    logOffset: 0,
    logSize: 10,

    loading: false
  }),
  actions: {
    async getPageListAction(pageName: string, queryInfo: any) {
      this.loading = true
      const pageUrl = `/${pageName}/list`
      const data = await getPageListData(pageUrl, queryInfo)
      this[`${pageName}PageContenList`] = data.data.list
      this[`${pageName}PageListTotal`] = data.data.totalCount
      this.loading = false
    },

    async deleteListAction(pageName: string, id: number) {
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl)
      this.getPageListAction(pageName, {
        offset: this[`${pageName}Offset`] * this[`${pageName}Size`],
        size: this[`${pageName}Size`]
      })
    },

    async createAction(pageName: string, userInfo: any) {
      const res = await createPageData(pageName, userInfo)
      this.getPageListAction(pageName, {
        offset: 0,
        size: 10
      })
      console.log(res)

      return res
    },

    async editAction(pageName: string, userInfo: any, userId: number) {
      // 1.编辑数据的请求
      const pageUrl = `/${pageName}/${userId}`
      await editPageData(pageUrl, userInfo)
      // 2.请求最新的数据
      this.getPageListAction(pageName, {
        offset: this[`${pageName}Offset`] * this[`${pageName}Size`],
        size: this[`${pageName}Size`]
      })
    },

    async uploadImgAction(pageName: string, id: any, data: any) {
      const url = `/upload/${pageName}_photo/${id}`
      await uploadImg(url, data)
      this.getPageListAction(pageName, {
        offset: 0,
        size: 10
      })
    },

    // 根据pagename来获取对应的offset和size
    getPageInfoByPageNameAction(pageName: string) {
      // 为了不手动创建这些key,当第一次如果发现没有将自动创建/不生效,可采纳Object.defineProperties
      // if (!this[`${pageName}PageContenList`]) {
      //   console.log('eeeeeee')
      //   console.log(`${pageName}PageContenList`)
      //   this[`${pageName}PageContenList`] = []
      // }
      // if (!this[`${pageName}PageListTotal`]) {
      //   this[`${pageName}PageListTotal`] = 0
      // }
      // if (!this[`${pageName}Offset`]) {
      //   this[`${pageName}Offset`] = 0
      // }
      // if (!this[`${pageName}Size`]) {
      //   this[`${pageName}Size`] = 10
      // }
      return {
        pageContenList: this[`${pageName}PageContenList`],
        pageListTotal: this[`${pageName}PageListTotal`],
        offset: this[`${pageName}Offset`],
        size: this[`${pageName}Size`]
      }
    },
    setPageInfoByPageNameAction(pageName: string, key: string, value: any) {
      switch (key) {
        case 'offset':
          this[`${pageName}Offset`] = value
          break
        case 'size':
          this[`${pageName}Size`] = value
          break
        default:
          break
      }
    }
  }
})
export default usePageContentStore
